package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type BehavioralTraitService struct{}

// CreateBehavioralTrait 创建行为人特质管理记录
// Author [yourname](https://github.com/yourname)
func (BTService *BehavioralTraitService) CreateBehavioralTrait(ctx context.Context, BT *flaud.BehavioralTrait) (err error) {
	err = global.GVA_DB.Create(BT).Error
	return err
}

// DeleteBehavioralTrait 删除行为人特质管理记录
// Author [yourname](https://github.com/yourname)
func (BTService *BehavioralTraitService) DeleteBehavioralTrait(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.BehavioralTrait{}, "id = ?", ID).Error
	return err
}

// DeleteBehavioralTraitByIds 批量删除行为人特质管理记录
// Author [yourname](https://github.com/yourname)
func (BTService *BehavioralTraitService) DeleteBehavioralTraitByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.BehavioralTrait{}, "id in ?", IDs).Error
	return err
}

// UpdateBehavioralTrait 更新行为人特质管理记录
// Author [yourname](https://github.com/yourname)
func (BTService *BehavioralTraitService) UpdateBehavioralTrait(ctx context.Context, BT flaud.BehavioralTrait) (err error) {
	err = global.GVA_DB.Model(&flaud.BehavioralTrait{}).Where("id = ?", BT.ID).Updates(&BT).Error
	return err
}

// GetBehavioralTrait 根据ID获取行为人特质管理记录
// Author [yourname](https://github.com/yourname)
func (BTService *BehavioralTraitService) GetBehavioralTrait(ctx context.Context, ID string) (BT flaud.BehavioralTrait, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&BT).Error
	return
}

// GetBehavioralTraitInfoList 分页获取行为人特质管理记录
// Author [yourname](https://github.com/yourname)
func (BTService *BehavioralTraitService) GetBehavioralTraitInfoList(ctx context.Context, info flaudReq.BehavioralTraitSearch) (list []flaud.BehavioralTrait, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.BehavioralTrait{})
	var BTs []flaud.BehavioralTrait
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["age_group"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&BTs).Error
	return BTs, total, err
}
func (BTService *BehavioralTraitService) GetBehavioralTraitPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
